package com.pedro.streamer.view;

import android.content.Context;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.pedro.streamer.R;
import com.pedro.streamer.adapter.AIVideoAdapter;
import com.pedro.streamer.manager.MediaPlayerClient;
import com.pedro.streamer.manager.VideoArrManager;
import com.pedro.streamer.manager.VideoControlManager;
import com.pedro.streamer.utils.FileUtils;
import com.pedro.streamer.utils.RecyclerViewSpacesItemDecoration;
import com.pedro.streamer.utils.SimpleItemTouchHelperCallback;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class VideoControlView extends ConstraintLayout implements View.OnClickListener {

    private String TAG = "VideoControlView";
    private Context mContext;
    private ConstraintLayout mediaLayout,clipLayout;
    private LinearLayout mediaOrderLayout, mediaRadomLayout, mediaSigleLayout;
    private VideoControlManager videoControlManager = VideoControlManager.getInstance();

    //videoManageView
    private RecyclerView choosVideoRecycleView;
    private static ArrayList<Uri> videoPicArr;
    private Uri curVideoPath;

    private  VideoArrManager videoArrManager = VideoArrManager.shareInstance();
    private static AIVideoAdapter aiVideoAdapter;

    public VideoControlView(@NonNull Context context) {
        super(context);
        mContext = context;
    }

    public VideoControlView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        initView();
    }

    private void initView() {

//        mediaOrderLayout = findViewById(R.id.media_order);
//        mediaRadomLayout = findViewById(R.id.media_radom);
//        mediaSigleLayout = findViewById(R.id.media_sigle);
//
//        mediaSigleLayout.setOnClickListener(this);
//        mediaRadomLayout.setOnClickListener(this);
//        mediaOrderLayout.setOnClickListener(this);


        mediaLayout = findViewById(R.id.media_set_layout);
        mediaLayout.setOnClickListener(this);

        clipLayout = findViewById(R.id.video_clip);
        clipLayout.setOnClickListener(this);

        choosVideoRecycleView = findViewById(R.id.choose_video_recycle);

        videoPicArr =  videoArrManager.getVideoArr();

        if (videoPicArr == null) {
            videoPicArr = new ArrayList<>();
        }

        aiVideoAdapter = new AIVideoAdapter();
        aiVideoAdapter.setVideoPicArr(videoPicArr);
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        stringIntegerHashMap.put(RecyclerViewSpacesItemDecoration.RIGHT_DECORATION, 20);//左间距


        choosVideoRecycleView.addItemDecoration(new RecyclerViewSpacesItemDecoration(stringIntegerHashMap));

        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        choosVideoRecycleView.setLayoutManager(layoutManager);
        choosVideoRecycleView.setAdapter(aiVideoAdapter);

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SimpleItemTouchHelperCallback(new SimpleItemTouchHelperCallback.ItemTouchHelperAdapter() {
            @Override
            public boolean onItemMove(int fromPosition, int toPosition) {
                if (fromPosition < toPosition) {
                    for (int i = fromPosition; i < toPosition; i++) {
                        Collections.swap(videoPicArr, i, i + 1);
                    }
                } else {
                    for (int i = fromPosition; i > toPosition; i--) {
                        Collections.swap(videoPicArr, i, i - 1);
                    }
                }
                aiVideoAdapter.notifyItemMoved(fromPosition, toPosition);
                VideoArrManager.shareInstance().setVideoArr(videoPicArr);

                return false;
            }
        }));
        itemTouchHelper.attachToRecyclerView(choosVideoRecycleView);
    }

    @Override
    public void onClick(View view) {

        int viewID = view.getId();
        if (viewID == R.id.media_set_layout){
            MediaPlayerClient.getInstance(mContext).showAudioView();
        } else if (viewID == R.id.video_clip) {
            MediaPlayerClient.getInstance(mContext).showClipView();

        }
//        switch (viewID) {
//            case R.id.media_set_layout:
//                MediaPlayerClient.getInstance(mContext).showAudioView();
//                break;
//            case R.id.media_order:
//                mediaOrderLayout.setSelected(true);
//                mediaRadomLayout.setSelected(false);
//                mediaSigleLayout.setSelected(false);
//                break;
//            case R.id.media_radom:
//                mediaOrderLayout.setSelected(false);
//                mediaRadomLayout.setSelected(true);
//                mediaSigleLayout.setSelected(false);
//                break;
//            case R.id.media_sigle:
//                mediaOrderLayout.setSelected(false);
//                mediaRadomLayout.setSelected(false);
//                mediaSigleLayout.setSelected(true);
//                break;
//            default:
//                break;
//        }

    }

    public void setVideoPicArr() {

        videoPicArr = videoArrManager.getVideoArr();
        aiVideoAdapter.setVideoPicArr(videoPicArr);
        aiVideoAdapter.notifyDataSetChanged();
    }



}
